// Copyright (C) joyinter
// All rights reserved.
// 
// Author : dengxinguo
// Date   : 2005-03-01


#include "StdAfx.h"
#include ".\joyaddrctr.h"

CJoyAddrCtr::CJoyAddrCtr(void)
{
}

CJoyAddrCtr::~CJoyAddrCtr(void)
{
}
void CJoyAddrCtr::setPlayerInfo(JOYP2PPACK p2pPack)
{
	PLAYERADDR playAddr;
	if(!m_PlayerAddr.Lookup(p2pPack.vip,playAddr))
	{
		return ;
	}else
	{
		memcpy(playAddr.sa_netnum,p2pPack.lc_netnum,4);
		memcpy(playAddr.sa_nodenum,p2pPack.lc_nodenum,6);
		m_PlayerAddr.SetAt(p2pPack.vip,playAddr);
	}

}
void CJoyAddrCtr::addPlayerAddrToMap(PLAYERADDR playaddr)
{
	m_PlayerAddr.SetAt(playaddr.nPlayerVirIP,playaddr);
}
bool CJoyAddrCtr::getUDPAddr(const short nVirIP,PLAYERADDR &playaddr)
{
   if(!m_PlayerAddr.Lookup(nVirIP,playaddr))
	{
		return false;
	}else
	{
		
		return true;
	
	}
	
}
bool CJoyAddrCtr::getUDPAddr(const short nVirIP,DWORD &ip,WORD &port)
{
	PLAYERADDR playaddr;
	if(!m_PlayerAddr.Lookup(nVirIP,playaddr))
	{
		return false;
	}else
	{
		
		ip=playaddr.dwPlayerRealIP;
		port=playaddr.wPlayerRealPort;
	
	}
	return true;
}
bool CJoyAddrCtr::getUDPAddr(const char *sa_noden,const char *sa_netn,short &nVirIP,DWORD &ip,WORD &port)
{
	POSITION pos;
	PLAYERADDR addr;

	pos=getFirstPosition();
	if(pos==NULL)
		return false;
	
	for(;;)
	{
		if(pos==NULL)
		{
			return false;
		}else
		{
			getNextAddr(pos,nVirIP,addr);
			if(strcmp(sa_noden,addr.sa_nodenum)==0&&strcmp(sa_netn,addr.sa_netnum)==0)
			{
				ip=addr.dwPlayerRealIP;
				port=addr.wPlayerRealPort;
				return true;
			}			
		}
	}
}
bool CJoyAddrCtr::getIPXAddr(const short nVirIP,char *sa_nodenum,char *sa_netnum,unsigned short *sa_socket)
{
	PLAYERADDR playaddr;
	if(!m_PlayerAddr.Lookup(nVirIP,playaddr))
	{
		return false;
	}else
	{
		memcpy(sa_nodenum,playaddr.sa_nodenum,4);
		memcpy(sa_netnum,playaddr.sa_netnum,6);
		*sa_socket=playaddr.sa_socket;
		
	}
	return true;
}
void CJoyAddrCtr::delPlayerAddrByIP(short nVirIP)
{
	m_PlayerAddr.RemoveKey(nVirIP);

}